const Card = require('./card.js');
const GameState = require('./status.js');
module.exports = DDZ

const enumStatus = GameState.statusID;

var shuffle = function(arr) {
    for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
    return arr;
};

function DDZ( tableid ){
	this._players = [];		// 保存玩家信息
	this._askInfo = [];		// 叫分数据
	this._dzList  = null;	// 抢地主的人选
	this._dzid = 0;			// 地主id
	this._chip = 0;			// 底分
	this._tableid = tableid;
	this._state = GameState.createState(0);
}
DDZ.prototype.getName = function() {
	return 'ddz table';
}
DDZ.prototype.getTableID = function() {
	return this._tableid;
}
DDZ.prototype.getStateID = function() {
	return this._state.id;
};
DDZ.prototype.setState = function(stateId) {
	process.nextTick(function(table){
		// 设置新状态.
		if(table._state != null){
			table._state.onExit();
		}		
		table._state = GameState.createState(stateId);
		console.log('设置状态',GameState.getStateName(stateId) );
		table._state.onEnter(table);
	},this);
};
DDZ.prototype.start = function(p1,p2,p3,cb) {
	this._players[0] = p1;
	this._players[1] = p2;
	this._players[2] = p3;
	this.onStart = cb;
	this.restart();
}
DDZ.prototype.restart = function( ) {
	this._askInfo = [];		// 叫分数据
	this._dzid = 0;			// 地主id
	this._chip = 0;			// 底分
	this._playID = 0;
	this.deal();

	console.log(enumStatus);
	// 设置为叫分状态
	this.setState(enumStatus.Bid);
};
DDZ.prototype.canNotLoss = function( ) {
	return false;
}
// 叫分, chip为倍数 0表示不要, 3表示叫3分, 4表示枪地主
// 返回: 成功返回true, 失败返回false
DDZ.prototype.askChip = function(seatId,chip){
	console.log('['+seatId+']叫分: '+chip)
	if(this.getStateID()!=enumStatus.Bid)
		return false;
	// 判断是否应该由当前玩家操作
	if(seatId!=this._playID){
		console.log('应该由当前玩家操作',this._playID)
		return false;
	}
	// var player = this._players[seatId];
	// if(player==null)
	// 	return false;

	// 要记录下哪些人叫了地主, 以便后面抢地主使用
	this._askInfo.push({'id':seatId,'chip':chip});
	if(chip>0){
		this._chip = chip;
		this.setState(enumStatus.Grab);
		return true;
	}
	this._playID = (this._playID+1)%3;
	// 如果一圈下来,都没有人叫地主
	if(this._askInfo.length==3){
		if(this.canNotLoss()){
			// 不能流局
			// 强制第一个可以叫地主的玩家1分叫地主
		}
		else{
			// 设置流局,流局后会重新发牌
			this.setState(enumStatus.Loss);
		}
	}
	return true;
}
// 抢地主
DDZ.prototype.askDZ = function(seatId,dz) {

	var n = this._dzList.length;
	var id = this._dzList[this._playID];
	if(seatId!=id)
		return;
	this._playID = (this._playID+1)%n;
	this._askInfo.push(id);

	if(dz>0) {
		this._dzid = id;
		this._chip *= 2;
		console.log(id + '抢地主. 当前底分:' + this._chip);
	}

	if(this._askInfo.length == n) {
		console.log('设置为地主, 传底牌给地主',this._dzid)
		var dz = this._players[this._dzid];
		// 设置为地主, 传底牌给地主
		dz.setDZ(this._holes);
		this._players[0].sort();
		this._players[1].sort();
		this._players[2].sort();

		this.setState(enumStatus.Playing);
	}
}

// 发牌
DDZ.prototype.deal = function() {
	var cards = this._getCards();
	console.log('洗牌:',cards);

	// 抽三张底牌
	this._holes =  [];
	this._holes.push(cards.pop());
	this._holes.push(cards.pop());
	this._holes.push(cards.pop());
	console.log('三张底牌:',this._holes)

	function getCards(id){
		console.log('发牌',id,id*17,id*17+17 );
		var cc = new Array();
		for (var i = id*13; i < id*13+13; i++) {
			cc.push(cards[i]);
		}
		console.log(id,cc)
		return cc;
	}
	this._players[0].setCards(getCards(0));
	this._players[1].setCards(getCards(1));
	this._players[2].setCards(getCards(2));
}
//返回一副扑克
DDZ.prototype._getCards = function( ) {
	var cardList = new Array();
	for (var i = 0; i < 54; i++) {
		cardList.push(new Card(i));
	}
	console.log(cardList);
	return shuffle(cardList);
	//return cardList;
}

DDZ.prototype.play = function( pid,idxList ) {
	if(idxList.length==0)
		return;
	if(this._lastHands != null){
		if (this._lastHands.cards.length != idxList.length)
			return "出牌的张数必须一致"; 	// 出牌的张数必须一致
	}

	var id = this._playID%3; // 0 1 2
	var player = this._players[id];

	var cs = player.getCards(idxList)
	
	//cs = [new Card(3),new Card(3),new Card(3),new Card(2),new Card(1)];
	var hands = Card.parseCardType(cs);
	if( hands==null )
		return "牌型错误";

	console.log('出牌:',hands);
	if (this._lastHands!=null) {
		if( hands.typeId!=this._lastHands.typeId )
			return '牌型不一致';

		var rt = Card.compareCardType(this._lastHands,hands);
		if(!rt)
			return '小于上家';
		console.log('是否大于上家',rt)
	}
	player.play(idxList);

	if(Card.cardType.wangzha == hands.typeId){
		// 如果是王炸,肯定没人要得起了. 继续由当前玩家出牌.
		this._lastHands = null;
	}
	else{
		this._lastHands = hands;
		this._playID = this._playID+1;
	}
	
	return hands;
};